package hedspi.k55.sm.view;

import hedspi.k55.sm.R;
import hedspi.k55.sm.model.Portfolio;
import hedspi.k55.sm.model.Stock;
import hedspi.k55.sm.model.Transaction;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockActivity;

public class PortfolioActivity extends SherlockActivity implements
		OnNavigationListener {
	private String[] list_source = { "COMPANY_NAME", "STOCK_CODE", "SHARES",
			"CURRENT_PRICE", "CHANGE_PRICE" };
	private int[] list_destination = { R.id.portfolio_item_companny_name,
			R.id.portfolio_item_stock_code, R.id.portfolio_item_shares,
			R.id.portfolio_item_current_price, R.id.portfolio_item_price_change };
	ListView listview;
	List<Stock> stockList;
	Portfolio portfolio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.portfolio_inside_activity);

		Context context = getSupportActionBar().getThemedContext();
		ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
				context, R.array.menu_list, R.layout.menu_list_item);
		list.setDropDownViewResource(R.layout.menu_dropdown_list_item);
		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		getSupportActionBar().setListNavigationCallbacks(list, this);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
		getSupportActionBar().setSelectedNavigationItem(1);
		listview = (ListView) findViewById(R.id.portfolio_list_view);
		getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		Stock aapl = new Stock(this, "aapl");
		Stock goog = new Stock(this, "goog");
		aapl.refresh();
		goog.refresh();
		aapl.setMyShares("10");
		goog.setMyShares("20");

		aapl.update();
		goog.update();
		portfolio = new Portfolio(this);
		portfolio.init();
		stockList = portfolio.getAll();
		Transaction trans = new Transaction(this, Transaction.BUY, "GOOG",
				"10/04/2013", 100, 200, 10, "abc");
		trans.add();
		List<Transaction> trans_list = goog.getAllTransaction();
		System.out.println(trans_list.get(0).getStockSymbol());
		// stockList.add(goog);
		viewList();
	}

	public void viewList() {

		List<HashMap<String, String>> filmaps = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < stockList.size(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("COMPANY_NAME", stockList.get(i).getCompanyName());
			map.put("STOCK_CODE", stockList.get(i).getSymbol());
			map.put("SHARES", "" + stockList.get(i).getMyShares());
			map.put("CURRENT_PRICE", stockList.get(i).getLastPrice());
			map.put("CHANGE_PRICE", stockList.get(i).getPercChange());
			filmaps.add(map);
		}
		SimpleAdapter sadp = new SimpleAdapter(this, filmaps,
				R.layout.portfolio_stock_item, list_source, list_destination);
		listview.setAdapter(sadp);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long id) {
				Intent detail = new Intent(PortfolioActivity.this,
						StockDetailActitivy.class);
				detail.putExtra("symbol", stockList.get(position).getSymbol());
				startActivity(detail);

			}
		});

	}

	public void viewStatistic() {

	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		switch (itemPosition) {
		case 0:
			Intent overview = new Intent(PortfolioActivity.this,
					OverviewActivity.class);
			startActivity(overview);
			finish();
			break;
		case 1:
			break;
		case 2:
			Intent transaction = new Intent(PortfolioActivity.this,
					TransactionActivity.class);
			startActivity(transaction);
			finish();
			break;
		default:
			break;
		}
		return true;
	}

}
